package com.ehotting.edsta.circuitDesign.gen.element.script;

import com.ehotting.edsta.circuitDesign.domain.EsCircuitCodeDataTemplate;
import com.ehotting.edsta.circuitDesign.domain.EsSceneParam;
import com.ehotting.edsta.circuitDesign.gen.DataTypeConvertHelper;
import com.ehotting.edsta.circuitDesign.gen.ScriptConstants;
import com.ehotting.edsta.circuitDesign.gen.element.helper.ElementScriptHelper;
import com.ehotting.edsta.circuitDesign.groovy.Groovy;
import com.ehotting.edsta.circuitDesign.service.EsElementService;
import com.ehotting.edsta.circuitDesign.service.IEsSceneParamService;
import com.ehotting.eaf.core.utils.StringUtils;
import com.ehotting.eaf.security.service.TokenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public abstract class ElementScriptAbstract implements ElementScriptAble {

    protected  Logger logger = LoggerFactory.getLogger(this.getClass());



    @Autowired
    protected IEsSceneParamService esSceneParamService;
    @Autowired
    protected ElementScriptHelper elementScriptHelper;
    @Autowired
    protected EsElementService esComponentService;
    @Autowired
    protected TokenService tokenService;
    @Autowired
    protected Groovy groovy;


    protected String functionStart(String functionName, List<EsCircuitCodeDataTemplate> inputParamList){
        StringBuffer buf = new StringBuffer();
        buf.append("def ");
        buf.append(elementScriptHelper.genMethodNameAndArgument(functionName,inputParamList));
        buf.append("{"+ ScriptConstants.LINE);
        return buf.toString();
    }



    protected List<EsSceneParam> findXElementList(String content,List<EsSceneParam> outputParamList){
        List<EsSceneParam> list = new ArrayList<>();
        for(EsSceneParam param : outputParamList){
            if(content.indexOf(param.getCode())!=-1){
                if(!list.contains(param)){
                    list.add(param);
                }
            }
        }
        return list;
    }


    protected String startIf(String content, String tab){
        content = content.replaceAll("==","=");
        String[] contents = content.split(";");
        StringBuffer buf = new StringBuffer();

        for(int i = 0;i< contents.length;i++){
            String c = contents[i];
            if(StringUtils.isBlank(c)){
                continue;
            }
            String[] ss = c.split("=");

            String s_0= ElementScriptHelper.replaceNodeToUnderline(ss[0]);
            String s_1= ElementScriptHelper.replaceNodeToUnderline(ss[1]);

            boolean isContainsOperator = ElementScriptHelper.isContainsOperator(s_1);
            buf.append(tab+"def diff_"+i+" = Math.abs("+s_0+" - ") ;
            if(isContainsOperator) {
                buf.append("(");
            }
            buf.append(s_1) ;
            if(isContainsOperator) {
                buf.append(")");
            }
            buf.append(");") ;

            buf.append(ScriptConstants.LINE);
        }

        buf.append(tab+"def diff = ") ;
        for(int i = 0;i< contents.length;i++){
            String c = contents[i];
            if(i>0){
                buf.append(" + ") ;
            }
            buf.append("diff_"+i);
        }
        buf.append(";");
        buf.append(ScriptConstants.LINE);
        buf.append( tab+"if(");
        for(int i = 0;i< contents.length;i++){
            if(StringUtils.isBlank(contents[i])){
                continue;
            }
            if(i>0){
                buf.append( " && ");
            }
            buf.append("diff_"+i+" <= ElementDataHelper.DIFF_VALUE");
        }
        buf.append( "){");
        buf.append(ScriptConstants.LINE);
        return buf.toString();
    }


    /***
     * 获取当前esSceneParam.getCode()带属性表达式的值,单行多个值使用逗号分开
     * 比如:当前esSceneParam.getCode()为r1,表达式为
     * r1.xx=1;r2=2;
     * 打出来是r1_xx=1;r2=2;
     *
     * @param esSceneParam
     * @param content
     * @param tab
     * @return
     */
    protected String getPropValue(EsSceneParam esSceneParam, String content, String tab){
        StringBuffer buf = new StringBuffer();
        Set<String> codeList = ElementScriptHelper.filterByCode(content,esSceneParam.getCode());
        for(String code : codeList){
            //logger.info("__code:{}",code);
            String[] cc = code.split("\\.");
            buf.append(tab);
            buf.append("double " + code.replaceAll("\\.", "_") + " = "+cc[0] + "Obj.prop(\""+cc[1]+"\");");
        }
        return buf.toString();
    }


    protected String startFor(String content, List<EsSceneParam> outputParamList){
        StringBuffer buf = new StringBuffer();
        for (int i=0;i<outputParamList.size();i++){
            EsSceneParam esSceneParam = outputParamList.get(i);
            buf.append(ScriptConstants.TAB(i+1));
            buf.append( "for(EsElement "+esSceneParam.getCode()+"Obj : "+esSceneParam.getCode()+"List){");
            buf.append( ScriptConstants.LINE);
            buf.append(ScriptConstants.TAB(i+2));

            buf.append( "double "+esSceneParam.getCode()+" = DataTypeConvertHelper.convertValue("+ esSceneParam.getCode() + "Obj)" +";");

            //buf.append( "def "+esSceneParam.getCode()+" = "+ esSceneParam.getCode() + "Obj.getValue()" +";");
//            buf.append( ScriptConstants.LINE);
//            buf.append(ScriptConstants.TAB(i+2));
//            buf.append( "System.out.println("+esSceneParam.getCode()+");");
            buf.append( ScriptConstants.LINE);
            buf.append( getPropValue( esSceneParam, content, ScriptConstants.TAB(i+2)));
        }
        return buf.toString();
    }

    protected String startFor(List<EsSceneParam> outputParamList){
        StringBuffer buf = new StringBuffer();
        for (int i=0;i<outputParamList.size();i++){
            EsSceneParam esSceneParam = outputParamList.get(i);
            buf.append( ScriptConstants.TAB(i+1));
            buf.append( "for(EsElement "+esSceneParam.getCode()+"Obj : "+esSceneParam.getCode()+"List){");
            buf.append( ScriptConstants.LINE);
        }
        return buf.toString();
    }





    public String handleSetValue(List<EsSceneParam> outputParamList,int i){
        StringBuffer buf = new StringBuffer();
        buf.append(ScriptConstants.TAB(i+2));
        buf.append( "java.util.Map<String,Object> map = new java.util.HashMap();"+ ScriptConstants.LINE);

        for (EsSceneParam esSceneParam : outputParamList){
            buf.append(ScriptConstants.TAB(i+2));
            buf.append( "map.put(\""+esSceneParam.getCode()+"\","+esSceneParam.getCode()+"Obj);"+ ScriptConstants.LINE);
        }
        buf.append(ScriptConstants.TAB(i+2) + "map.put(\""+ScriptConstants.DIFF+"\",diff);"+ ScriptConstants.LINE);

        buf.append(ScriptConstants.TAB(i+2));
        buf.append( "componentList.add(map);"+ ScriptConstants.LINE);
        return buf.toString();
    }



    protected String endFor(int i){
        StringBuffer buf = new StringBuffer();
        for (;i>0;){
            buf.append(ScriptConstants.TAB(i--));
            buf.append( "}"+ ScriptConstants.LINE);
        }
        return buf.toString();
    }


}
